package cn.lyf.dao;

import cn.lyf.entity.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AnswerDaoImpl implements AnswerDao {
    @Autowired
    MongoTemplate mongoTemplate;

    //添加回复
    @Override
    public Answer addAnswerDao(Answer answer) {
        System.out.println("answer==");
        System.out.println(answer);
      return  mongoTemplate.save(answer);
    }

    //根据博客文章id查找对应的博客的评论集合
    @Override
    public List<Answer> findAnswersByBlogid(String blogid){
        Query query = new Query(Criteria.where("blogid").is(blogid));
        query.with(Sort.by(Sort.Order.desc("tcreate")));
        List<Answer> answers = mongoTemplate.find(query,Answer.class);
        return answers;
    }

    //根据id查找评论
    @Override
    public Answer findById(String id) {
        System.out.println("id=="+id);
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query,Answer.class);
    }
}
